import { xml2js } from 'xml-js';

import historyList from 'src/script/file/history';

import file from '../../file//file';
import translateItem from '../../file/translateItem';
import plugin from '../../plugins/plugin';
/**
 *  rimWorld 语言文件
 *  {
 *      文件类型： xml,
 *      结构: {
 *          `<?xml version="1.0" encoding="utf-8"?>
 *              <LanguageData>
 *                  <文本key>文本</文本key>
 *              </LanguageData>
 *          `
 *      }
 *  }
 */

let template = `
<?xml version="1.0" encoding="utf-8"?>
<LanguageData>
  <key>[Value]</key>
</LanguageData>
`;

class rimWorld implements plugin {
  pluginName: string = 'rimWorld';
  pluginType: string = 'xml';
  public registerPlugin(): plugin {
    return this;
  }
  public buildFile(name: string, v: any) {
    let data = xml2js(v);
    let t: translateItem[] = [];
    let f = new file(name, this.pluginType, t);
    data.elements[0].elements.forEach((e: any) => {
      if (e.name !== undefined && e.elements !== undefined) {
        let key = e.name;
        let value = e.elements[0].text;
        let item = new translateItem(
          key,
          value,
          '',
          false,
          new historyList([])
        );
        f.addContent(item);
      }
    });
    return f;
  }
}
let plugin = new rimWorld();
export = plugin;
